

cc.Class({
    extends: cc.Component,

    properties: {
    //倒计时进度条
    timeprogressBar:cc.ProgressBar,
    //游戏时间文本
    timelable:cc.Label,
    //主角节点
    master:cc.Node,
    //地刺预制件
    diciprefab:cc.Prefab,
    //加载地刺父节点
    dicinode:cc.Node,
    //加载分数
    score:cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.gamescore = 0;
        this.count = 0;
        //游戏总时间
        this.totaltime = 60;
        this.remaintime = this.totaltime;
        //启动倒计时
     this.schedule(function(){
        this.remaintime--; 
        //时间比例
        this.timelable.string = "time:" + this.remaintime;
        if(this.remaintime >= 0){
            var ratio = this.remaintime / this.totaltime;
            this.timeprogressBar.progress = ratio; 
        }else{
            
        cc.director.loadScene("over");
        }
      
     },1);

     //为屏幕添加点击事件
     this.node.on("touchstart",this.inputcontrol.bind(this));

    for(let i = 0; i < 8; i++){
        this.spawndici();

    }
    

    },
   
    //触摸事件回调
    inputcontrol:function(touch,event){
    this.gamescore++;
    this.score.string = "score:" + this.gamescore;

    cc.data.game_score = this.gamescore;

    cc.log(touch.getLocation());
    //确定点击位置处于屏幕哪一侧
    var isclickleft = touch.getLocation().x < cc.winSize.width / 2;
    var isinleft = this.master.x < 0;
    this.onjump(isclickleft == isinleft,isinleft);

    //this.schedule(this.spawnpipe.bind(this),2);
    this.spawndici();

    },
  
    spawndici:function(){
    var dici = cc.instantiate(this.diciprefab);
    //this.menu.parent.addChild(pipes);
    this.dicinode.addChild(dici);
    //打印位置
    //cc.log(this.dicinode.position);
   
    var isleft = (Math.random() > 0.5)?1:-1;
    dici.x *= isleft;
    dici.scaleX = isleft;
    if(this.count<8){
         //120是地刺刚开始生成摆放到精灵之后合适的位置,地刺之间间隔为142
        dici.y = 120 - this.count*142;
    }else{
        dici.y = 120 - 7*142;
    } 
    this.count++;
    },


    onjump:function(isJumpInPlace,isinleft){
        var t = isinleft? 1:-1;
    //原地跳
    var JumpInPlace = cc.sequence(cc.moveBy(0.1,cc.p(50*t,0)),cc.moveBy(0.1,cc.p(-50*t,0)));
    var JumptoOtherside = cc.moveBy(0.1,cc.p(468*t,0));
    var JumptoOtherside = cc.spawn(cc.moveBy(0.1,cc.p(468*t,0)),cc.rotateBy(0,0,180));
    if(isJumpInPlace){

       this.master.runAction(JumpInPlace);   
          
   }else{

     this.master.runAction(JumptoOtherside);

   }
 
    },
  
     update (dt) {

     },
});
